import { usePltI18nStore } from '@/stores/i18n-plt'
const pltI18nStore = usePltI18nStore()

const { taskName, startTime, duration} = pltI18nStore.pltI18n.cisdi.gczx.frontEnd.gantt

import { useUserStore } from '@/stores/user.js'
const store = useUserStore()

export const riskGanttColor = (item) => {
  const { CC_WBS_PROGRESS_STATUS_ID } = item.valueMap
  if (CC_WBS_PROGRESS_STATUS_ID == 'TODO') {
    return '#ebebeb'
  } else if (CC_WBS_PROGRESS_STATUS_ID == 'DOING') {
    return '#eec871'
  } else if (CC_WBS_PROGRESS_STATUS_ID == 'DONE') {
    return '#63b47e'
  } else if (CC_WBS_PROGRESS_STATUS_ID == 'OVER') {
    return '#e60012'
  } else {
    return '#ebebeb'
  }
}

// 甘特图配置项及渲染
export const renderGantt = (ganttDom, tasks) => {
  gantt.config.date_format = '%Y-%m-%d'
  gantt.config.columns = [
    { name: 'text', tree: true, label: taskName, width: '*', min_width: 250, resize: true },
    {
      name: 'start_date',
      label: startTime,
      min_width: 100,
      align: 'center',
    },
    {
      name: 'duration',
      label: duration,
      width: 80,
      align: 'center',
    },
  ]
  gantt.templates.task_class = (start, end, task) => {
    // console.log('🚀 ~ getData ~ task:', task)
    switch (task.status) {
      case 'TODO':
        return 'todo'
      case 'DOING':
        return 'doing'
      case 'DONE':
        return 'done'
      case 'OVER':
        return 'over'
      default:
        return 'todo'
    }
  }
  gantt.config.readonly = true // 只读模式
  var hourToStr = gantt.date.date_to_str('%H:%i')
  var hourRangeFormat = function (step) {
    return function (date) {
      var intervalEnd = new Date(gantt.date.add(date, step, 'hour') - 1)
      return hourToStr(date) + ' - ' + hourToStr(intervalEnd)
    }
  }
  gantt.config.min_column_width = 80
  // gantt.config.grid_width = 300
  // gantt.config.scroll_size = 20
  // gantt.config.scroll_align = 'right'
  // gantt.config.scrollbar = {
  //   size: 50,
  //   mousewheel: true,
  //   hide: false,
  //   scroll: false,
  // }
  var zoomConfig = {
    minColumnWidth: 40,
    maxColumnWidth: 150,
    levels: [
      // 缩放比例支持月，取消注释后即可
      // [
      //   { unit: 'year', format: '%Y', step: 1 },
      //   { unit: 'month', format: '%M', step: 1 },
      // ],
      [
        { unit: 'year', format: '%Y %M', step: 1 },
        {
          unit: 'week',
          step: 1,
          format: function (date) {
            var dateToStr = gantt.date.date_to_str('%m%d')
            var endDate = gantt.date.add(date, 6, 'day')
            return dateToStr(date) + ' ~ ' + dateToStr(endDate)
          },
        },
      ],
      [
        { unit: 'month', format: '%M %Y', step: 1 },
        { unit: 'day', format: '%d %M', step: 1 },
      ],
      [
        { unit: 'day', format: '%d %M', step: 1 },
        { unit: 'hour', format: hourRangeFormat(12), step: 12 },
      ],
      [
        { unit: 'day', format: '%d %M', step: 1 },
        { unit: 'hour', format: hourRangeFormat(6), step: 6 },
      ],
      [
        { unit: 'day', format: '%d %M', step: 1 },
        { unit: 'hour', format: '%H:%i', step: 1 },
      ],
    ],
    startDate: new Date('2024-01-01'),
    endDate: new Date('2029-01-01'),
    useKey: 'ctrlKey',
    trigger: 'wheel',
    element: function () {
      return gantt.$root.querySelector('.gantt_task')
    },
  }
  gantt.ext.zoom.init(zoomConfig)
  gantt.i18n.setLocale(store?.loginInfo?.currentLangId === 'ZH_CN' ? 'cn' : '') // store.loginInfo.currentLangId
  gantt.init(ganttDom)
  // 更新数据
  gantt.clearAll() // 清除当前所有任务
  gantt.parse(tasks)
  gantt.eachTask(function (task) {
    task.$open = true // 默认展开全部任务
  })
  gantt.render() // 如果需要，可以使用以下代码来调整布局和渲染
}
